﻿using System;
using Kaleida.ServiceMonitor.Operations.ResponseHandlers;
using NUnit.Framework;

namespace Kaleida.UnitTests.Operations.ResponseHandlers
{
    [TestFixture]
    public class MustMatchTests
    {
        [Test]
        public void TestStringArgument()
        {
            Assert.AreEqual("Regex matched document of length 3 characters", new MustMatch("").ProcessResponse("abc"));
            Assert.AreEqual("Regex matched document of length 1 character", new MustMatch("1").ProcessResponse("1"));
            Assert.AreEqual("Regex matched document of length 3 characters", new MustMatch("123").ProcessResponse("123"));
            Assert.AreEqual("Regex matched document of length 8 characters", new MustMatch("-123").ProcessResponse(" -123456"));
            Assert.AreEqual("Regex matched document of length 20 characters", new MustMatch(@"line1\r\nline2").ProcessResponse(" line1\r\nline2\r\nline3"));
            Assert.AreEqual("Regex matched document of length 6 characters", new MustMatch("abcdef").ProcessResponse("abcdef"));

            Assert.AreEqual("Regex matched document of length 3 characters", new MustMatch("[0-9]+").ProcessResponse("123"));
            Assert.AreEqual("Regex matched document of length 2 characters", new MustMatch("[0-9]+").ProcessResponse("12"));
            Assert.AreEqual("Regex matched document of length 1 character", new MustMatch("[0-9]+").ProcessResponse("1"));

            Assert.AreEqual("Expected response to match regular expression '[0-9]+'", Assert.Throws<InvalidOperationException>(() => new MustMatch("[0-9]+").ProcessResponse(" Abcd")).Message);

            Assert.AreEqual("Expected response to match regular expression 'abc'", Assert.Throws<InvalidOperationException>(() => new MustMatch("abc").ProcessResponse(" Abcd")).Message);
        }

        [Test]
        public void TestStringContainerArgument()
        {
            Assert.AreEqual("Regex matched document of length 3 characters", new MustMatch("").ProcessResponse(new MockStringContainer("abc")));
            Assert.AreEqual("Regex matched document of length 1 character", new MustMatch("1").ProcessResponse(new MockStringContainer("1")));
            Assert.AreEqual("Regex matched document of length 3 characters", new MustMatch("123").ProcessResponse(new MockStringContainer("123")), "Content is used in preference to ToString()");
            Assert.AreEqual("Regex matched document of length 8 characters", new MustMatch("-123").ProcessResponse(new MockStringContainer(" -123456")), "Content is used in preference to ToString()");
            Assert.AreEqual("Regex matched document of length 20 characters", new MustMatch(@"line1\r\nline2").ProcessResponse(new MockStringContainer(" line1\r\nline2\r\nline3")));
            Assert.AreEqual("Regex matched document of length 6 characters", new MustMatch("abc").ProcessResponse(new MockStringContainer("abcdef")), "Content is used in preference to ToString()");

            Assert.AreEqual("Regex matched document of length 3 characters", new MustMatch("[0-9]+").ProcessResponse(new MockStringContainer("123")));
            Assert.AreEqual("Regex matched document of length 2 characters", new MustMatch("[0-9]+").ProcessResponse(new MockStringContainer("12")));
            Assert.AreEqual("Regex matched document of length 1 character", new MustMatch("[0-9]+").ProcessResponse(new MockStringContainer("1")));

            Assert.AreEqual("Expected response to match regular expression '[0-9]+'", Assert.Throws<InvalidOperationException>(() => new MustMatch("[0-9]+").ProcessResponse(new MockStringContainer(" Abcd"))).Message);

            Assert.AreEqual("Expected response to match regular expression 'abc'", Assert.Throws<InvalidOperationException>(() => new MustMatch("abc").ProcessResponse(new MockStringContainer(" Abcd"))).Message);
        }
    }
}
